﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using BEGDC.CC.Web.UserControls;
using BEGDC.CC.BL.CCFlow.CR;
using BEGDC.CC.Entity;
using BEGDC.CC.BL.Workflow;
using BEGDC.CC.BL.Common;
using BEGDC.Utility.Castle;

namespace BEGDC.CC.Web.Common
{
	/// <summary>
	/// Summary description for AccompanyInput.
	/// </summary>
	public class CostCenterForCV : System.Web.UI.Page
	{
		#region Variables
		protected System.Web.UI.WebControls.TextBox tbName;
		protected System.Web.UI.WebControls.TextBox tbTitle;
		protected System.Web.UI.WebControls.TextBox tbActualCost;
		protected System.Web.UI.WebControls.TextBox tbTransferCost;
		protected System.Web.UI.WebControls.Button Btn_Save;
		protected System.Web.UI.WebControls.Button Btn_Close;
		protected System.Web.UI.HtmlControls.HtmlTableRow tr_actualCost;
		protected System.Web.UI.HtmlControls.HtmlTableRow tr_actualtransferCost;
		#endregion

		private void Page_Load(object sender, System.EventArgs e)
		{
			string CostID = Request["CostID"];

			if(CostID!=null)
			{
				this.CostID = Convert.ToInt32(CostID);
			}
			else
			{
				this.CostID = 0;
			}

			if(!this.IsPostBack)
			{
				this.Btn_Save.Attributes["onclick"] = "javascript:OnSave();";
				this.Btn_Close.Attributes["onclick"] = "javascript:OnClose();";
				//this.Btn_Save.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path+"/images/bbw_save.gif";
				///this.Btn_Close.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path+"/images/bbw_close.gif";

				if(this.CostID>0)
				{
					InitForm();
				}

				if(Request.QueryString["Actual"] == null || Request.QueryString["Actual"] == string.Empty)
				{
					this.tr_actualCost.Visible = false;
					this.tbActualCost.Text="0";
				}
				if(Request.QueryString["Actual"] != null && Request.QueryString["Actual"] == "OCO")
				{
					this.tr_actualtransferCost.Visible = true;
				}
				else
				{
					this.tr_actualtransferCost.Visible = false;
					this.tbTransferCost.Text = "0";
				}

			}
		}

		#region CostID
		protected int CostID
		{
			get{ return (int)this.ViewState["costID"]; }
			set{ this.ViewState["costID"] = value; }
		}
		#endregion

		#region ObtainAttendeeFromDataTable
		private DataRow ObtainAttendeeFromDataTable(DataTable datatable)
		{
			if(datatable==null||datatable.Rows.Count==0||this.CostID==0) return null;

			string IdStr = null;
			for(int i=0;i<datatable.Rows.Count;i++)
			{
				IdStr = datatable.Rows[i]["Id"].ToString();
				int IdInt = Convert.ToInt32(IdStr);
				if(IdInt==this.CostID)
				{
					return datatable.Rows[i];
				}
			}
			return null;
		}
		#endregion

		#region InitForm
		private void InitForm()
		{
			DataTable costInfo = null;
			DataRow row = null;
			object sessCostInfo = Session[SessionFlag.CostCenter];
			if(sessCostInfo!=null)
			{
				costInfo = (DataTable)sessCostInfo;
			}
			else
			{
				return;
			}
			
			if(this.CostID > 0)
			{
				row = costInfo.Rows[this.CostID - 1];

				this.tbName.Text = row["CostCenterCode"].ToString();
				this.tbTitle.Text = row["Cost"].ToString();
				this.tbActualCost.Text = row["ActualCost"].ToString();
				this.tbTransferCost.Text = row["ActualTransferCost"].ToString();
			}
			
		}
		#endregion

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.Btn_Save.Click += new EventHandler(Btn_Save_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		#region Update Accompany Session
		private void UpdateCostCenterSession()
		{
			DataTable dataTable = null;
			DataRow dataRow = null;
			object SessCostCenter = Session[SessionFlag.CostCenter];
			if(SessCostCenter!=null)
			{
				dataTable = (DataTable)SessCostCenter;
			}
			else
			{
				return;
			}
			
			if(this.CostID == 0)
			{
				dataRow = dataTable.NewRow();
				dataRow["CostCenterCode"] = this.tbName.Text;
				dataRow["Cost"] = this.tbTitle.Text;
				dataRow["ActualCost"] = this.tbActualCost.Text;
				dataRow["ActualTransferCost"] = this.tbTransferCost.Text;

				dataTable.Rows.Add(dataRow);
			}
			else if(this.CostID > 0)
			{
				dataRow = dataTable.Rows[this.CostID - 1];

				dataRow["CostCenterCode"] = this.tbName.Text;
				dataRow["Cost"] = this.tbTitle.Text;
				dataRow["ActualCost"] = this.tbActualCost.Text;
				dataRow["ActualTransferCost"] = this.tbTransferCost.Text;
			}
			else
			{
				return;
			}
			
			Session[SessionFlag.CostCenter] = SetRowID(dataTable);
		}
		#endregion

		#region Save
        void Btn_Save_Click(object sender, EventArgs e)
		{
			try
			{
				UpdateCostCenterSession();
			}
			catch(Exception exp)
			{
				throw exp;
			}
			Response.Write("<script>window.close();window.returnValue='reload';</script>");
		}
		#endregion

		private DataTable SetRowID(DataTable table)
		{
			if(table != null && table.Rows.Count >0)
				for(int i=1; i<=table.Rows.Count; i++)
				{
					table.Rows[i-1][0] = i.ToString();
				}
			return table;
		}
	}
}
